package org.dromara.heweather.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.dromara.common.core.constant.HttpStatus;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.ip.RegionUtils;
import org.dromara.common.heweather.api.HeWeatherApi;
import org.dromara.common.heweather.dto.CitySearchDTO;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.heweather.vo.HomeHeWeatherVO;
import org.dromara.quick.chat.domain.UserIpLocation;
import org.dromara.quick.chat.service.UserIpLocationService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 和风天气
 * @Author: Stars
 * @Date: 2025/4/4 20:06
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/he/weather")
public class HeWeatherController {

    private final HeWeatherApi heWeatherApi;

    private final UserIpLocationService userIpLocationService;

    /**
     * 实时天气查询
     * @param location
     * @return
     * @throws IOException
     */
    @RequestMapping("/now")
    public R<?> weatherNow(String location, HttpServletRequest request) throws IOException {
        if (StringUtils.isEmpty(location)) {
            return R.fail("请输入城市");
        }
        Long userId = LoginHelper.getUserId();
        String clientIp = RegionUtils.getClientIp(request);
        String province = RegionUtils.extractProvince(clientIp);
        if(StringUtils.isEmpty(province)) {
            return R.fail(HttpStatus.WARN, "获取IP地址失败");
        }
        // 设置用户的IP地址，注意：这里只需要设置省份即可，GPS定位不能存储，用户隐私
        UserIpLocation userIpLocation = new UserIpLocation();
        userIpLocation.setIpAddress(clientIp);
        userIpLocation.setIpLocation(province);
        userIpLocation.setUserId(userId);
        userIpLocation.setLastUpdateTime(new Date());
        userIpLocationService.insertOrUpdateUserIpLocation(userIpLocation);
        return R.ok(heWeatherApi.weatherNow(location));
    }

    /**
     * 天气预报首页查询
     */
    @RequestMapping("/home")
    public R<?> home(String location) throws IOException {
        if (StringUtils.isEmpty(location)) {
            return R.fail("请输入城市");
        }
        List<CitySearchDTO> citySearchList = heWeatherApi.searchCity(location);
        HomeHeWeatherVO homeHeWeatherVO = new HomeHeWeatherVO();
        if (CollectionUtils.isNotEmpty(citySearchList)){
            homeHeWeatherVO.setCitySearchDTO(citySearchList.get(0));
        }
        homeHeWeatherVO.setWeatherNowDTO(heWeatherApi.weatherNow(location));
        homeHeWeatherVO.setDailyWeatherForecastList(heWeatherApi.dailyWeather(location));
        homeHeWeatherVO.setHourlyWeatherForecastList(heWeatherApi.hourlyWeatherForecast(location));
        homeHeWeatherVO.setWeatherIndexList(heWeatherApi.weatherIndex(location, "1,3,5,7,8,9,10,11,12,13,14,15,16"));
        return R.ok(homeHeWeatherVO);
    }
}
